//
//  PsdTradeCodeVerify.swift
//  LeGou
//
//  Created by lxm on 2022/8/1.
//

import UIKit

class PsdTradeCodeVerify: ZMViewController, XMCodeInputViewDelegate {
    
    //MARK:--------------------属性--------------------
    @IBOutlet weak var codeView: XMCodeInputView!

    var phone : String = "解绑第三方账户"
    
    var openId : String?
    
    var onComplete : ((_ text : String)->())?
    
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var loginBtn: UIButton!
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    
    //MARK:--------------------UI--------------------
    func setUI() {
        title = ""
        codeView.inputCount = 6
        codeView.space = 8
        let con = XMCodeInputCellConfig()
        con.hideLine = true
        con.borderColor = "#EAEAEA".zm.hex()
        con.borderWidth = 1
        con.securityInput = true
        con.cornerRadius = 8
        con.backGourndColor = .white
        codeView.cellConfig = con
        codeView.delegate = self
        codeView.reaload()
        codeView.collectionView.backgroundColor = "#F5F5F5".zm.hex()
        codeView.becomeFirstResponder()
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        codeView.textField.rx.text.orEmpty.map({$0.count == 6}).subscribe(onNext:{ [unowned self] resp in
            self.loginBtn.isEnabled = resp
            self.loginBtn.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
    }
    
    // 重写返回按钮
    override func customBackButton() -> UIButton? {
        let btn = ZMUtil.button(title: "返回", image: UIImage.init(named: "back_18"), font: UIFont.systemFont(ofSize: 18), color: .black)
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
        btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 0)
        return btn
    }
    
    @IBAction func onLogin(_ sender: Any) {
        if let onComplete = onComplete {
            onComplete(codeView.text)
        }
    }

    //MARK:--------------------delegate--------------------
    func inputComplete(_ text: String) {
        print(text)
       
    }
}
